/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.email;

import java.security.Security;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Wei Liu
 */
public class EmailSender
{

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String EMAIL_FROM_ADDRESS = "ticket.sales.distribution@gmail.com";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    public EmailSender()
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    }

    public void sendSSLMessage(Email email)
            throws AddressException, javax.mail.MessagingException
    {
        boolean debug = true;

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
        {

            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("ticket.sales.distribution@gmail.com", "cics505termproject");
            }
        });

        session.setDebug(debug);

        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(EMAIL_FROM_ADDRESS);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[email.getEmailAddress().size()];

        Iterator<String> iterator = email.getEmailAddress().iterator();
        int i = 0;
        while (iterator.hasNext())
        {
            addressTo[i++] = new InternetAddress(iterator.next());
        }

//        for (int i = 0; i < emails.length; i++)
//        {
//            addressTo[i] = new InternetAddress(emails[i].getEmailAddress());
//        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(email.getSubject());
        msg.setContent(email.getMessage(), "text/html");

        Thread thread = new SendMailThread(msg);
        thread.start();

        //Transport.send(msg);
    }
}

class SendMailThread extends Thread
{

    Message msg;

    SendMailThread(Message msg)
    {
        this.msg = msg;
    }

    @Override
    public void run()
    {
        try
        {
            Transport.send(msg);
        }
        catch (MessagingException ex)
        {
            Logger.getLogger(SendMailThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
